{extend name="Public:base" /}

<!-- 标题 -->
{block name="title"}<title>权限列表</title>{/block}

<!-- 面包屑导航 -->
{block name="navigation"}
<section class="content-header">
	<h1>权限列表<small></small></h1>
	<ol class="breadcrumb">
		<li><a href="{:url('index/index')}"><i class="fa fa-dashboard"></i> 主页</a></li>
		<li class="active">权限管理</li>
	</ol>
</section>
{/block}

<!-- 内容 -->
{block name="content"}
<!-- Custom Tabs -->
<div class="nav-tabs-custom">
	
	<ul class="nav nav-tabs">
		<li class="active"><a href="#tab_1" data-toggle="tab">权限列表</a></li>
		<li><a href="{:url('manager/rulesAdd')}">添加权限</a></li>
		<li class="pull-right"><a href="#" class="text-muted" ><i class="fa fa-gear"></i></a></li>
	</ul>
	
	<div class="tab-content">
		<div class="tab-pane active" id="tab_1">
			<form class="form-inline" action="{:url('manager/rulesSort')}" method="post">
			<!-- datatable -->
			<table id="example" class="table table-striped table-bordered table-hover"  cellpadding="0" cellspacing="0" border="0"  width="100%">
				 <thead>
					<tr>	
						<th width="5%">排序</th>
						<th width="5%">ID</th>
						<th width="50%">名称</th>
						<th width="15%">标识</th>
						<th width="10%">状态</th>
						<th width="10%">操作</th>
					</tr>
				</thead>
 
			</table><!-- /datatable -->
			</form>
			<button class="btn btn-success" id="sortBtn" type="submit">排序</button>	 
		</div><!-- /.tab-pane -->
 
	</div><!-- /.tab-content -->
</div><!-- nav-tabs-custom -->
 

{/block}

<!-- js -->
{block name="js"}
 
	<!-- page script -->
	<script src="{$Think.WWW_ROOT}/public/static/admin/js/dataTables.js" type="text/javascript"></script>
    <script type="text/javascript">
        var table = null ;
        $(document).ready( function () {
			
            table = $('#example').dataTable( {
				"processing": true,//加载提示
				"serverSide": true,//服务端模式
				"searching": false,
				"paging": false,
				"ajax": {
					"url": "{:url('manager/rulesList')}",
					"type": "POST",
					"data":{'mycolumns':['sorts','id','title','name','status']},
				},
				//展示的列
				"columns": [
					{ "data": "sorts"},
                    { "data": "id"},
                    { "data": "title" },
                    { "data": "name" },
					{ "data": "status" },
					{ "data": "id" },
                ],
				//设置列排序
				"order": [[ 0, "asc" ]],
				//列设置
				"columnDefs": [
					{
						"targets": [ 0 ],
						"orderable": true,
						"createdCell": function (td, cellData, rowData, row, col) {
							$(td).html("<input type='text' style='width:50px;' class='form-control input-sm ' name='listOrder["+rowData['id']+"]' value='" + cellData + "'>");
						}
					},
					{
						"targets": [ 2],
						"orderable": false,
						"render": function(data, type, row) {
							return '<a href="{$Think.WWW_ROOT}/public/index.php/admin/manager/rulesEdit/id/'+row['id']+'">'+row['html']+data +'</a>';
			            },
						
					},
					{
						"targets": [ 3],
						"orderable": false
					},

					{
						"targets": [ 5 ],
						"orderable": false,
						"render": function(data, type, row) {
							var btn ='<a href="{$Think.WWW_ROOT}/public/index.php/admin/manager/rulesEdit/id/'+row['id']+'" class="btn btn-primary btn-xs btn-flat" >编辑</a>'
									+ '&nbsp;'
									+'<a href="javascript:void(0);" onclick="deleteFun('+row['id']+')" class="btn btn-danger btn-xs btn-flat" >删除</a>';	
							return btn;
			            },
						
					},

				],
 
 
            } );
			
			//排序
			$('#sortBtn').on("click", function () {
				var action = $('form').attr("action");
				var data = $('form').serialize();
				$.post(action,data,success, "json");
				function success(data){
					if (data.status) {
						layer.msg(data.msg, {
							icon:1,
							offset: 0,
							shift: 0,
							time:1500
						},function(){
							table.api().draw(false); 
						});
						
					} else {
						layer.msg(data.msg, {
							icon:0,
							offset: 0,
							shift: 6,
							time:1500
						});
					}
				}
			});
			 
             
        } );
 
		//表单提交
    	$(document)
	    	.ajaxStart(function(){
	    		$("#sortBtn").addClass("btn-default").removeClass('btn-primary').attr("disabled", true);
	    	})
	    	.ajaxStop(function(){
	    		$("#sortBtn").removeClass("btn-default").addClass("btn-primary").attr("disabled", false);
	    	});
	  
	  
		function deleteFun(id){
			layer.confirm('确定删除该记录ID='+id+'吗？', {
				btn: ['确认','取消'], //按钮
				shade: false //不显示遮罩
			}, function(){
				 _deleteFun(id); 
			}, function(){
				 
			});
			 
		}
		
		 
		
		/**
		* 删除
		* @param id
		* @private
		*/
		function _deleteFun(id) {
			
			$.ajax({
				url: "{:url('manager/rulesDel')}",
				data: {"id": id},
				type: "post",
				success: function (data) {
					if (data.status) {
						layer.msg(data.msg, {
							icon:1,
							offset: 0,
							shift: 0,
							time:1500
						},function(){
							table.api().draw(false); 
						});
						
					} else {
						layer.msg(data.msg, {
							icon:0,
							offset: 0,
							shift: 6,
							time:1500
						});
					}
				}, 
				error: function (error) {
					alert(data.msg);
				}
			});
		}
		 
    </script>
 {/block}